package com.wang.interview.reference;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * @author: wang
 * @date: 2021/8/12 21:47
 * @description:虚引用 PhantomReference ：任何时候都可能被回收，get的永远是 null，只能配合 ReferenceQueue 使用
 * 引用队列只是存的引用变量，而不是引用指向的对象，所以，引用对象get返回的只是一个没有实际意义的地址，remove 方法阻塞
 *
 */
public class T2 {
    public static void main(String[] args) throws InterruptedException {
        ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
        Object o = new Object();
        WeakReference<Object> weakReference = new WeakReference<Object>(o);

        System.out.println(o);
        System.out.println(weakReference.get());
        System.out.println(referenceQueue.poll());
        System.out.println("================================");
        o = null;
        System.gc();
        System.out.println(o);
        System.out.println(weakReference.get());
        System.out.println(referenceQueue.poll());
        System.out.println("================================");
        referenceQueue.remove();
        System.out.println(referenceQueue);
    }
}
